package com.example.gesturetest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnGestureListener {

	private GestureDetector gDetector;
	private Button buttonNext;
	private final OnClickListener onClickListenerButtonNext = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(MainActivity.this,
					AnimationViewActivity.class);
			startActivity(intent);
		}
	};

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		getWindow().getAttributes().windowAnimations = R.style.SlideDiagonal;

		setContentView(R.layout.activity_main);
		buttonNext = (Button) findViewById(R.id.button_next);
		buttonNext.setOnClickListener(onClickListenerButtonNext);

		gDetector = new GestureDetector(this);

	}

	@Override
	public boolean onDown(MotionEvent arg0) {
		return false;
	}

	@Override
	public boolean onFling(MotionEvent start, MotionEvent finish,
			float xVelocity, float yVelocity) {

		Display display = ((WindowManager) getSystemService(MainActivity.WINDOW_SERVICE))
				.getDefaultDisplay();

		double interval_Y = start.getRawY() - finish.getRawY();
		interval_Y = interval_Y < 0 ? interval_Y * (-1) : interval_Y;
		double interval_X = start.getRawX() - finish.getRawX();
		interval_X = interval_X < 0 ? interval_X * (-1) : interval_X;

		boolean up_left = (start.getRawY() > finish.getRawY())
				&& (start.getRawX() > finish.getRawX());

		boolean up_right = (start.getRawY() > finish.getRawY())
				&& (start.getRawX() < finish.getRawX());
		boolean down_left = (start.getRawY() < finish.getRawY())
				&& (start.getRawX() > finish.getRawX());

		boolean down_right = (start.getRawY() < finish.getRawY())
				&& (start.getRawX() < finish.getRawX());

		@SuppressWarnings("deprecation")
		boolean reta = (interval_X * 100 / display.getWidth() <= 32)
				|| (interval_Y * 100 / display.getHeight() <= 22);

		if (reta) {
			if (interval_Y > interval_X) {
				if (start.getRawY() < finish.getRawY())
					((ImageView) findViewById(R.id.image_place_holder))
							.setImageResource(R.drawable.down);
				else
					((ImageView) findViewById(R.id.image_place_holder))
							.setImageResource(R.drawable.up);

			} else {
				if (start.getRawX() < finish.getRawX())
					((ImageView) findViewById(R.id.image_place_holder))
							.setImageResource(R.drawable.right);
				else
					((ImageView) findViewById(R.id.image_place_holder))
							.setImageResource(R.drawable.left);
			}

		} else {

			if (up_left)
				((ImageView) findViewById(R.id.image_place_holder))
						.setImageResource(R.drawable.up_left);
			else if (up_right)
				((ImageView) findViewById(R.id.image_place_holder))
						.setImageResource(R.drawable.up_right);
			else if (down_left)
				((ImageView) findViewById(R.id.image_place_holder))
						.setImageResource(R.drawable.down_left);
			else if (down_right)
				((ImageView) findViewById(R.id.image_place_holder))
						.setImageResource(R.drawable.down_right);

		}

		return false;
	}

	@Override
	public void onLongPress(MotionEvent arg0) {
	}

	@Override
	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent arg0) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent arg0) {
		return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gDetector.onTouchEvent(event);
	}

}
